//
//  YTTextView.m
//  Elife
//
//  Created by 拓客营 on 2022/4/16.
//

#import "YTTextView.h"
#import "FSTextView.h"

@interface YTTextView()<UITextViewDelegate>


@property (nonatomic, copy, nullable) NSString *placeholder;
@property (nonatomic, assign) NSInteger length;

@property (nonatomic, strong) UITextView *contentTextView;
@property (nonatomic, strong) UILabel *contentNumLab;//内容字数

@property (nonatomic, strong) FSTextView *textView;

@end

@implementation YTTextView

-(instancetype)initWithFrame:(CGRect)frame placeholder:(NSString *)placeholder MaxLength:(NSInteger)length {
    if (self = [super initWithFrame:frame]) {
        self.placeholder = placeholder;
        self.length = length;
        [self UIConfig];
    }
    return self;
}


-(void)UIConfig {
    
    FSTextView *textView = [FSTextView textView];
    self.textView = textView;
    textView.cornerRadius = 6;
    textView.backgroundColor = kRGB(0xEFEFEF);
    textView.placeholder = self.placeholder;
    // 限制输入最大字符数.
    if (self.length != 0) {
        textView.maxLength = self.length;
    }
    // 添加输入改变Block回调.
    WeakSelf
    [textView addTextDidChangeHandler:^(FSTextView *textView) {
        // 文本改变后的相应操作.
        [weakSelf changeMaxLengthLab:textView];
        weakSelf.textValue = textView.text;
    }];
    // 添加到达最大限制Block回调.
    [textView addTextLengthDidMaxHandler:^(FSTextView *textView) {
        // 达到最大限制数后的相应操作. // 传出处理后的text
        weakSelf.textValue = textView.formatText;
    }];
    [self addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    
    [self addSubview:self.contentNumLab];
    [self.contentNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-12);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-5);
        make.height.mas_equalTo(20);
    }];
    
}



#pragma mark - UITextViewDelegate
-(void)textViewDidEndEditing:(UITextView *)textView {
//    if (textView.text.length == 0) {
//        self.contentTextView.text = @" ";
//    }
    
    self.textValue = textView.text;
}


- (void)changeMaxLengthLab:(UITextView *)textView {
    
    //不让显示负数
    UITextRange *selectedRange = [textView markedTextRange];
    //获取高亮部分
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
    //如果在变化中是高亮部分在变，就不要计算字符了

    if (selectedRange && pos) {

        return;
    }
    NSString *nsTextContent = textView.text;
    NSInteger existTextNum = nsTextContent.length;

    NSString *finalStr = @"";
    if (existTextNum > self.length+1) {

        //截取到最大位置的字符(由于超出截部分在should时被处理了所在这里这了提高效率不再判断)
//        [AlertUtils error:@"不能超过200字"];
        NSString *s = [nsTextContent substringToIndex:self.length];
        [textView setText:s];
        
//
        
        finalStr = [NSString stringWithFormat:@"%ld/%ld", s.length, self.length];
        
    } else {
        finalStr = [NSString stringWithFormat:@"%ld/%ld", existTextNum, self.length];
    }
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc]initWithString:finalStr];
    NSRange separeRange = [finalStr rangeOfString:@"/"];
    [attri addAttribute:NSForegroundColorAttributeName value:kRGB(0xFA2F2F) range:NSMakeRange(0, separeRange.location)];
    
    self.contentNumLab.attributedText = attri;

}

#pragma mark - set get
-(void)setBgColor:(UIColor *)bgColor {
    _bgColor = bgColor;
    self.textView.backgroundColor = bgColor;
}

-(void)setFont:(UIFont *)font {
    _font = font;
    _contentNumLab.font = font;
    _textView.font = font;
}

-(void)setCornerRadius:(CGFloat)cornerRadius {
    _cornerRadius = cornerRadius;
    _textView.cornerRadius = cornerRadius;
}

-(void)setPlaceholderColor:(UIColor *)placeholderColor {
    _placeholderColor = placeholderColor;
    _textView.placeholderColor = placeholderColor;
}

-(void)setPlaceholderFont:(UIFont *)placeholderFont {
    _placeholderFont = placeholderFont;
    _textView.placeholderFont = placeholderFont;
}

-(void)setHiddenCountLab:(BOOL)hiddenCountLab {
    _hiddenCountLab = hiddenCountLab;
    self.contentNumLab.hidden = hiddenCountLab;
}
-(void)setValueConfig:(NSString *)valueConfig {
    _valueConfig = valueConfig;
    _textView.text = valueConfig;
}

#pragma mark - lazy
-(UITextView *)contentTextView {
    if (!_contentTextView) {
        _contentTextView = [[UITextView alloc]initWithFrame:CGRectZero];
        _contentTextView.delegate = self;
        _contentTextView.backgroundColor = kRGB(0xEFEFEF);
        _contentTextView.layer.cornerRadius = 6;
        _contentTextView.clipsToBounds = YES;
        _contentTextView.textColor = kRGB(0x3D444D);
        _contentTextView.text = @" ";
    }
    return _contentTextView;
}

-(UILabel *)contentNumLab {
    if (!_contentNumLab) {
        _contentNumLab = [UILabel new];
        _contentNumLab.textColor = kRGB(0x999999);
        _contentNumLab.font = [UIFont systemFontOfSize:14];
        
        NSString *finalStr = [NSString stringWithFormat:@"0/%ld",self.length];
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc]initWithString:finalStr];
        NSRange separeRange = [finalStr rangeOfString:@"/"];
        [attri addAttribute:NSForegroundColorAttributeName value:kRGB(0xFA2F2F) range:NSMakeRange(0, separeRange.location)];
        
        _contentNumLab.attributedText = attri;
    }
    
    return _contentNumLab;
}

@end
